<?php

header("Content-Type:text/html;charset=utf-8;");



class Curl
{

    // 默认设置
    private static $defaults = [
        // 是否为post请求
        CURLOPT_POST => 1,
        CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1',
    ];

    // 连接句柄
    private $ch;
    private $response;

    // 构造函数
    function __construct($url, array $params = array(), array $cookies = array(), array $options = array())
    {
        $this->Init($url, $params, $cookies, $options);
    }

    function __destruct()
    {
        curl_close($this->ch);
    }

    // 初始化函数
    public function init($url, array $params = array(), array $cookies = array(), array $options = array())
    {
        $this->ch = curl_init($url);



        if ( count($params) != 0)
        {
            $options[CURLOPT_POSTFIELDS] = http_build_query($params);
        }

        if ( count($cookies) != 0)
        {
            $options[CURLOPT_COOKIE] = http_build_cookie($cookies);
        }

        curl_setopt_array($this->ch, $options + Curl::$defaults);
    }

    public function setOptions(array $options)
    {
        if ($options == NULL || count($options) == 0)
            return;
        curl_setopt_array($this->ch, $options);
    }

    public function setParams(array $paramsArr)
    {
        if ($paramsArr == NULL || count($paramsArr) == 0)
            return;
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, http_build_query($paramsArr));
    }

    public function dev()
    {
        $this->setOptions([
            CURLOPT_HEADER => true,
            CURLOPT_RETURNTRANSFER => true,
        ]);
    }

    public function undev()
    {
        $this->setOptions([
            CURLOPT_HEADER => false,
            CURLOPT_RETURNTRANSFER => false,
        ]);
    }

    public function exec()
    {
        if( ! $this->response = curl_exec($this->ch))
        {
            trigger_error(curl_error($this->ch));
        }
        return $this->response;
    }

    public function getCookies()
    {
        preg_match_all('/Set-Cookie: (.+)=(.+)$/m', $this->response, $cookiesStr);
        $cookiesStr = explode(':',$cookiesStr[0][0])[1];
        $cookiesStr = explode(";", $cookiesStr);

        $cookies = [];
        foreach($cookiesStr as $key => $value)
        {
            $value = explode("=",$value);
            @$cookies[$value[0]] = $value[1];
        }
        return $cookies;
    }

    public function setCookies(array $cookies)
    {
        foreach($cookies as $key => $value)
        {
            $cookies[$key] = $key. "=" . $value;
        }
        $cookies = join(";",$cookies);
        $this->setOptions([ CURLOPT_COOKIE => $cookies]);
    }

    public function setUrl($url)
    {
        $this->setOptions([CURLOPT_URL => $url]);
    }

    public function getCh()
    {
        return $this->ch;
    }

    static function post($url, array $parmas = NULL, array $options = array())
    {
        $ch = curl_init($url);
        if ( $parmas != NULL || count($parmas) != 0)
        {
            $options[] = "CURLOPT_POSTFIELDS:" . http_build_query($parmas);
        }

        curl_setopt_array($ch, ($options + Curl::$defaults));
        if( ! $result = curl_exec($ch))
        {
            trigger_error(curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }


    static function get($url, array $get = NULL, array $options = array())
    {

        $ch = curl_init($url);
        if ( $get != NULL || count($get) != 0)
        {
            $options[] = "CURLOPT_URL:" . $url. (strpos($url, '?') === FALSE ? '?' : ''). http_build_query($get);
        }


        curl_setopt_array($ch, ($options + Curl::$defaults));
        if( ! $result = curl_exec($ch))
        {
            trigger_error(curl_error($ch));
        }
        curl_close($ch);
        return $result;
    }
}


?>
